package cn.shujuhai.common.controller.permission;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.shujuhai.common.aop.SysLog;
import cn.shujuhai.common.domain.SysPermission;
import cn.shujuhai.common.pluging.data.ResultVo;
import cn.shujuhai.common.service.SysPermissionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: NiuYaHu
 * @Date: 2024/12/5 12:01
 * @Description: 针对权限的控制层操作
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/permission")
public class SysPermissionController {
    final SysPermissionService sysPermissionService;

    /**
     * 获取权限列表
     *
     * @return 权限列表集合
     */
    @SysLog(value = "权限列表查询", operationType = "查询", module = "权限模块")
    @GetMapping("/list")
    public ResultVo<?> getPermissionList() {
        return this.sysPermissionService.getPermissionList();
    }

    /**
     * 新增权限
     *
     * @param sysPermission 新增权限对象
     * @return
     */
    @SysLog(value = "新增权限", operationType = "新增", module = "权限模块")
    @PostMapping("/add")
    public ResultVo<?> addPermission(@Validated @RequestBody SysPermission sysPermission) {
        return this.sysPermissionService.savePermission(sysPermission);

    }

    /**
     * 根据权限Id删除权限
     *
     * @param permissionId 权限Id
     * @return 删除成功或失败的结果及响应信息
     */
    @SysLog(value = "删除权限", operationType = "编辑", module = "权限模块")
    @PostMapping("/delete/{permissionId}")
    public ResultVo<?> removePermissionById(@PathVariable("permissionId") Long permissionId) {
        return this.sysPermissionService.removePermissionById(permissionId);
    }


    /**
     * 权限启用禁用
     *
     * @param permissionId 权限Id
     * @return 权限启用或禁用的状态值
     */
    @SaCheckPermission("permission_enable")
    @SysLog(value = "权限启用禁用", operationType = "编辑", module = "权限模块")
    @PostMapping("/enable/{permissionId}")
    public ResultVo<SysPermission> enablePermission(@PathVariable Long permissionId) {
        return this.sysPermissionService.enablePermission(permissionId);
    }

    /**
     * 根据权限Id获取权限列表
     *
     * @param permissionId 权限Id
     * @return 权限集合
     */
    @SaCheckPermission("menu_list")
    @SysLog(value = "根据权限Id获取权限列表", operationType = "查询", module = "权限模块")
    @GetMapping("/getPermissionByPermissionId/{permissionId}")
    public ResultVo<?> getPermissionByPermissionId(@PathVariable Long permissionId) {
        return this.sysPermissionService.getPermissionByPermissionId(permissionId);
    }

    /**
     * 权限编辑
     *
     * @param sysPermission 编辑权限对象
     * @return 更新后的权限对象
     */
    @SysLog(value = "更新权限", operationType = "编辑", module = "权限模块")
    @PostMapping("/update")
    public ResultVo<?> updatePermission(@RequestBody @Validated SysPermission sysPermission) {
        return this.sysPermissionService.updatePermission(sysPermission);
    }

    /**
     * 权限的模糊查询
     *
     * @param permissionName 权限名称
     * @return 返回父级权限集合
     */
    @SaCheckPermission("permission_list")
    @SysLog(value = "权限的模糊查询", operationType = "查询", module = "权限模块")
    @GetMapping("/getParent")
    public ResultVo<?> getParent(@Validated String permissionName) {
        return this.sysPermissionService.getPermissionsWithParentsByNameLike(permissionName);
    }

    /**
     * 根据权限Id查询角色Id
     *
     * @param permissionId 权限Id
     * @return 返回角色列表
     */
    @SaCheckPermission("permission_list")
    @SysLog(value = "根据权限Id查询角色Id", operationType = "查询", module = "权限模块")
    @GetMapping("/getRoleIdsByPermissionId")
    public ResultVo<?> getRoleIdsByPermissionId(Long permissionId) {
        return this.sysPermissionService.getRoleIdsByPermissionId(permissionId);
    }

}